package bloodfang;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

public class StatBlock {
	private final double[] data;
    
    public StatBlock() {
        data = new double[Stat.values().length];
    }
    
    public StatBlock(Stat stat, double d){
    	data = new double[Stat.values().length];
    	setStat(stat,d);
    }
    
    public StatBlock(Stat stat, double d, Stat stat2, double d2){
    	data = new double[Stat.values().length];
    	setStat(stat,d);
    	setStat(stat2,d2);
    }
    
    public StatBlock(JSONObject wowheadData) {
    	data = new double[Stat.values().length];
    	try{
	        for (Stat stat : Stat.values()) {
	            if (wowheadData.has(stat.getWowheadName())) {
	                data[stat.getIndex()] = wowheadData.getInt(stat.getWowheadName());
	            } else {
	                data[stat.getIndex()] = 0;
	            }
	        }
    	}catch(JSONException e){
    		e.printStackTrace();
    	}
    }
    
    public double getStat(Stat stat) {
        return data[stat.getIndex()];
    }
    
    public void add(StatBlock other,double uptime) {
        int newData[] = new int[Stat.values().length];
        for (int i = 0; i < newData.length; ++i) {
            data[i] += other.data[i] * uptime;
        }
    }
    
    public void add(StatBlock other,double uptime, double scaling) {
        int newData[] = new int[Stat.values().length];
        for (int i = 0; i < newData.length; ++i) {
            data[i] += Math.round(other.data[i] * scaling) * uptime;
        }
    }
    
    public void add(StatBlock other) {
       add(other, 1);
    }
    
    public void setStat(Stat stat, double d) {
        data[stat.getIndex()] = d;
    }
    
    public void addStat(Stat stat, int value) {
        data[stat.getIndex()] += value;
    }
    
    public String toString() {
    	StringBuffer buf = new StringBuffer();
    	ArrayList<String> stats = new ArrayList<String>();
    	for (Stat stat : Stat.values()) {
    		if(data[stat.getIndex()] != 0){
    			stats.add(stat+"="+data[stat.getIndex()]);
    		}
    	}
    	buf.append(stats.toString());
    	return buf.toString();
    }

}
